package com.wxlgroup.springboot.service.serviceImp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxlgroup.springboot.dao.DepartmentDao;
import com.wxlgroup.springboot.entity.Department;
import com.wxlgroup.springboot.service.DepartmentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

@Service
public class DepartmentServiceImp implements DepartmentService {

    @Resource
    private DepartmentDao departmentDao;


    @Override
    public Object queryAllDepartments(String departmentName,int pageNumber,int pageSize) {
        if(!"".equals(departmentName)){
            departmentName="%"+departmentName+"%";
        }else {
            departmentName="%%";
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<Department> departments = departmentDao.queryAllDepartments(departmentName);
        for (Department department : departments) {
            department.setEmployeeCount(departmentDao.countEmployees(department.getDepartmentId()));
        }
        PageInfo<Department> pageInfo=new PageInfo<>(departments);
        return pageInfo;

    }

    @Override
    public int countEmployees(int departmentId) {
        return departmentDao.countEmployees(departmentId);
    }

    @Override
    public int updateDepartmentById(Department department) {
         return departmentDao.updateDepartmentById(department);
    }

    @Override
    public int queryDepartmentIdByName(String name) {
        return departmentDao.queryDepartmentIdByName(name);
    }
}
